import { list, listTeacher } from '../services/recommend';
import { listSpecial } from '../services/gather';

const types = require('../../../assets/stureg_web/static/types.json');

export default {
  namespace: 'recommend',

  state: {
    data: [],
    exportData: [],

    counts: {
      total: 0,
      recommend1: 0, // 经老师推荐人数
      recommend0: 0, // 未经老师推荐人数
    },

    teacherData: [], // 教师列表数据(树数据)

    sysData: [], // 系部数据
    specData: {}, // 专业数据
    mappingSpecialName: {}, // 根据专业id获取专业名称
    mappingSpecialPID: {}, // 根据专业id获取系部id
  },

  effects: {
    *list({ payload }, { call, put }) {
      const response = yield call(list, payload);
      yield put({ type: 'callbackList', response });
    },
    *listExport({ payload }, { call, put }) {
      const response = yield call(list, payload);
      yield put({ type: 'callbackList', response, isExport: true });
    },
    *listSpecial({ payload }, { call, put }) {
      const response = yield call(listSpecial, payload);
      yield put({ type: 'callbackListSpecial', response });
    },
    *listTeacher({ payload }, { call, put }) {
      const response = yield call(listTeacher, payload);
      yield put({ type: 'callbackListTeacher', response });
    },
  },

  reducers: {
    callbackList(st, { response, isExport }) {
      const state = st;
      const { success } = response;
      if (!success) return { ...state };

      let { data } = response;
      data = data.map((item) => {
        const { specialId, type } = item;
        let specialName = state.mappingSpecialName[specialId];
        specialName += ' - ' + types[type];

        return {
          ...item,
          specialName,
        };
      });

      const { counts } = response;
      state.counts = counts;
      if (isExport) state.exportData = data;
      else state.data = data;

      return { ...state };
    },
    callbackListSpecial(st, { response }) {
      const state = st;
      const { success = true, data = [] } = response;
      if (!success) return { ...state };

      const sysData = [], specData = {};
      const mappingSpecialName = {}; // 根据专业id获取专业名称
      const mappingSpecialPID = {}; // 根据专业id获取系部id

      data.map(item => {
        const { pid } = item;

        if (!pid) {
          // 系部
          sysData.push(item);
          return;
        }

        // 专业
        if (!specData[pid]) specData[pid] = [];
        specData[pid].push(item);
        const { id, name } = item;
        mappingSpecialName[id] = name;
        mappingSpecialPID[id] = pid;
      });

      state.sysData = sysData;
      state.specData = specData;
      state.mappingSpecialName = mappingSpecialName;
      state.mappingSpecialPID = mappingSpecialPID;
      return { ...state };
    },
    callbackListTeacher(st, { response }) {
      const state = st;
      const { success } = response;
      if (!success) return { ...state };

      const { data } = response;
      state.teacherData = data.map(([name, count, rank]) => ({
        title: name,
        key: rank ? name : '',
        count,
        rank,
      }));
      return { ...state };
    },
  },
};
